package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrComConfigBinding;
import com.swgk.core.view.entity.EnterpriseRecEntity;
import com.sgb.kjwl.view.ui.activity.manage.EnterpriseAndMapActivity;
import com.sgb.kjwl.viewmodel.home.HomeCompanyConfigModel;
import com.sgb.member.ui.activity.MemberInviteOrApplyActivity;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.CardUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.fragment.EnterpriseRecMenuFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 点击进入企业
 * LiWei 2021/10/29
 */
public class HomeCompanyConfigFragment extends AppLazyFragment {

    private FrComConfigBinding binding;
    private HomeCompanyConfigModel viewModel;

    public static HomeCompanyConfigFragment newInstance() {
        HomeCompanyConfigFragment dialog = new HomeCompanyConfigFragment();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_com_config, container, false);
        viewModel = new ViewModelProvider(getActivity()).get(HomeCompanyConfigModel.class);
        CardUtils.setCardShadowColor(binding.cardViewGy, getResources().getColor(R.color.color_title_big81), getResources().getColor(R.color.transparent));
        CardUtils.setCardShadowColor(binding.cardViewHy, getResources().getColor(R.color.color_title_big81), getResources().getColor(R.color.transparent));
        CardUtils.setCardShadowColor(binding.cardViewGz, getResources().getColor(R.color.color_title_big81), getResources().getColor(R.color.transparent));
        CardUtils.setCardShadowColor(binding.cardViewTj, getResources().getColor(R.color.color_title_big81), getResources().getColor(R.color.transparent));
        return binding.getRoot();
    }

    @Override
    protected void initView() {
//        viewModel.liveData1.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
//            @Override
//            public void onChanged(List<EnterpriseRecEntity> list) {
//                if (list.size() == 0) {
//                    binding.recyclerCompany.setVisibility(View.GONE);
//                    binding.tvDefaultHint.setVisibility(View.VISIBLE);
//                } else {
//                    binding.recyclerCompany.setVisibility(View.VISIBLE);
//                    binding.tvDefaultHint.setVisibility(View.GONE);
//                    initHomeMenu(list);
//                  /*  binding.homeCompanyMenu.initData(getParentFragmentManager(), list, new CompanyMenuView.OnItemClick() {
//                        @Override
//                        public void onItemClick(EnterpriseRecEntity entity, int position, int num) {
//                            super.onItemClick(entity, position, num);
//                        }
//                    });*/
//                }
//            }
//        });
        viewModel.liveData2.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0) {
                    binding.tvMember1DefaultHint.setVisibility(View.VISIBLE);
                } else {
                    initMember1Menu(list);
                }
            }
        });
        viewModel.liveData3.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0) {
                    binding.linMemberDefault.setVisibility(View.VISIBLE);
                    binding.btnMemberInvite.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MemberInviteOrApplyActivity.startInviteOrApply(getActivity(), 0);
                        }
                    });
                } else {
                    initMemberMenu(list);
                }
            }
        });
        viewModel.liveData4.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                if (list.size() == 0) {
                    binding.linAttentionDefault.setVisibility(View.VISIBLE);
                    binding.btnAttentionInvite.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
                            String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
                            EnterpriseAndMapActivity.start(getActivity(), longitude, latitude, false);
                        }
                    });
                } else {
                    initAttentionMenu(list);
                }
            }
        });
        viewModel.liveData5.observe(getActivity(), new Observer<List<EnterpriseRecEntity>>() {
            @Override
            public void onChanged(List<EnterpriseRecEntity> list) {
                initRecommendMenu(list);
            }
        });
    }

//    /**
//     * 首页企业菜单
//     *
//     * @param list
//     */
//    private void initHomeMenu(List<EnterpriseRecEntity> list) {
//        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
//        binding.recyclerCompany.setLayoutManager(manager);
//        if (getActivity() == null){
//            return;
//        }
//        EnterpriseRecAdapter adapter = new EnterpriseRecAdapter(getActivity(), list);
//        binding.recyclerCompany.setAdapter(adapter);
//    }

    @Override
    public void onVisible() {
//        viewModel.getCompanyList("1", false);
    }

    @Override
    protected void onLazyLoad() {
//        viewModel.getCompanyList("1", false);
        viewModel.getCompanyList("4", false);
        viewModel.getCompanyList("5", false);

        int TYPE = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (TYPE == 0) {
            binding.linMember1.setVisibility(View.GONE);
            viewModel.getCompanyList("3", false);
        }
        if (TYPE == 1) {
            viewModel.getCompanyList("2", false);
            viewModel.getCompanyList("3", false);
        }
        if (TYPE == 2 || TYPE == 3) {
            binding.linMember1.setVisibility(View.GONE);
            binding.linMember.setVisibility(View.GONE);
        }
    }

    public int COLUMN_COUNT = 3;
    public int ROW_COUNT = 3;
    private ArrayList<Fragment> member1MenuFragment = new ArrayList<>();
    private ImageView[] member1MenuTip;
    private int member1Page = 1;
    private ArrayList<Fragment> memberMenuFragment = new ArrayList<>();
    private ImageView[] memberMenuTip;
    private int memberPage = 1;
    private ArrayList<Fragment> attentionMenuFragment = new ArrayList<>();
    private ImageView[] attentionMenuTip;
    private int attentionPage = 1;
    private ArrayList<Fragment> recMenuFragment = new ArrayList<>();
    private ImageView[] recMenuTip;
    private int recPage = 1;


    /**
     * 成员企业菜单
     *
     * @param list
     */
    private void initMember1Menu(List<EnterpriseRecEntity> list) {
        if (member1MenuFragment == null) {
            member1MenuFragment = new ArrayList<>();
        }
        member1MenuFragment.clear();
        int total = list.size();
        member1Page = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < member1Page; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            member1MenuFragment.add(EnterpriseRecMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT));
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), member1MenuFragment);
        binding.consMember1ViewPage.setAdapter(menuAdapter);
        binding.consMember1ViewPage.setOffscreenPageLimit(member1Page);
        binding.consMember1ViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setMember1ImageBackgroundMenu(position % member1Page);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (member1Page > 1) {
            binding.dotMember1MenuLayout.setVisibility(View.VISIBLE);
            binding.dotMember1MenuLayout.removeAllViews();
            initMember1DotsMenu(0);
        } else {
            binding.dotMember1MenuLayout.removeAllViews();
            binding.dotMember1MenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initMember1DotsMenu(int selectItems) {
        member1MenuTip = new ImageView[member1Page];
        for (int i = 0; i < member1Page; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            member1MenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                member1MenuTip[i].setSelected(true);
            } else {
                member1MenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.dotMember1MenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setMember1ImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < member1MenuTip.length; i++) {
            if (i == selectItems) {
                member1MenuTip[i].setSelected(true);
            } else {
                member1MenuTip[i].setSelected(false);
            }
        }
    }

    /**
     * 会员企业菜单
     *
     * @param list
     */
    private void initMemberMenu(List<EnterpriseRecEntity> list) {
        if (memberMenuFragment == null) {
            memberMenuFragment = new ArrayList<>();
        }
        memberMenuFragment.clear();
        int total = list.size();
        memberPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < memberPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            memberMenuFragment.add(EnterpriseRecMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT));
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), memberMenuFragment);
        binding.consMemberViewPage.setAdapter(menuAdapter);
        binding.consMemberViewPage.setOffscreenPageLimit(memberPage);
        binding.consMemberViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setMemberImageBackgroundMenu(position % memberPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (memberPage > 1) {
            binding.dotMemberMenuLayout.setVisibility(View.VISIBLE);
            binding.dotMemberMenuLayout.removeAllViews();
            initMemberDotsMenu(0);
        } else {
            binding.dotMemberMenuLayout.removeAllViews();
            binding.dotMemberMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initMemberDotsMenu(int selectItems) {
        memberMenuTip = new ImageView[memberPage];
        for (int i = 0; i < memberPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            memberMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                memberMenuTip[i].setSelected(true);
            } else {
                memberMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.dotMemberMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setMemberImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < memberMenuTip.length; i++) {
            if (i == selectItems) {
                memberMenuTip[i].setSelected(true);
            } else {
                memberMenuTip[i].setSelected(false);
            }
        }
    }

    /**
     * 关注企业菜单
     *
     * @param list
     */
    private void initAttentionMenu(List<EnterpriseRecEntity> list) {
        if (attentionMenuFragment == null) {
            attentionMenuFragment = new ArrayList<>();
        }
        attentionMenuFragment.clear();
        int total = list.size();
        attentionPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < attentionPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            attentionMenuFragment.add(EnterpriseRecMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT));
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), attentionMenuFragment);
        binding.consViewPage.setAdapter(menuAdapter);
        binding.consViewPage.setOffscreenPageLimit(attentionPage);
        binding.consViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setAttentionImageBackgroundMenu(position % attentionPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (attentionPage > 1) {
            binding.dotMenuLayout.setVisibility(View.VISIBLE);
            binding.dotMenuLayout.removeAllViews();
            initAttentionDotsMenu(0);
        } else {
            binding.dotMenuLayout.removeAllViews();
            binding.dotMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initAttentionDotsMenu(int selectItems) {
        attentionMenuTip = new ImageView[attentionPage];
        for (int i = 0; i < attentionPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            attentionMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                attentionMenuTip[i].setSelected(true);
            } else {
                attentionMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.dotMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setAttentionImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < attentionMenuTip.length; i++) {
            if (i == selectItems) {
                attentionMenuTip[i].setSelected(true);
            } else {
                attentionMenuTip[i].setSelected(false);
            }
        }
    }

    private void initRecommendMenu(List<EnterpriseRecEntity> list) {
        if (list.size() == 0){
            binding.tvConsDefaultHint.setVisibility(View.VISIBLE);
            return;
        }else{
            binding.tvConsDefaultHint.setVisibility(View.GONE);
        }
        if (recMenuFragment == null) {
            recMenuFragment = new ArrayList<>();
        }
        recMenuFragment.clear();
        int total = list.size();
        recPage = total % (COLUMN_COUNT * ROW_COUNT) > 0 ? total / (COLUMN_COUNT * ROW_COUNT) + 1 : total / (COLUMN_COUNT * ROW_COUNT);
        for (int i = 0; i < recPage; i++) {
            List<EnterpriseRecEntity> menuEntityList = new ArrayList<>();
            int fir = i * COLUMN_COUNT * ROW_COUNT;
            int last = (i + 1) * COLUMN_COUNT * ROW_COUNT > total ? total : (i + 1) * COLUMN_COUNT * ROW_COUNT;
            menuEntityList.addAll(list.subList(fir, last));
            recMenuFragment.add(EnterpriseRecMenuFragment.newInstance(menuEntityList, COLUMN_COUNT, ROW_COUNT));
        }
        FragPagerAdapter menuAdapter = new FragPagerAdapter(getChildFragmentManager(), recMenuFragment);
        binding.recConsViewPage.setAdapter(menuAdapter);
        binding.recConsViewPage.setOffscreenPageLimit(recPage);
        binding.recConsViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                setRecImageBackgroundMenu(position % recPage);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        if (recPage > 1) {
            binding.recDotMenuLayout.setVisibility(View.VISIBLE);
            binding.recDotMenuLayout.removeAllViews();
            initRecDotsMenu(0);
        } else {
            binding.recDotMenuLayout.removeAllViews();
            binding.recDotMenuLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 根据传入数据和当前设置的布局行列的出来的页数布局标识
     */
    private void initRecDotsMenu(int selectItems) {
        recMenuTip = new ImageView[recPage];
        for (int i = 0; i < recPage; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(R.drawable.select_banner_dot_config);
            recMenuTip[i] = imageView;
            float w = getResources().getDimension(R.dimen.dp8);
            if (i == selectItems) {
                recMenuTip[i].setSelected(true);
            } else {
                recMenuTip[i].setSelected(false);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams((int) w,
                    (int) getResources().getDimension(R.dimen.dp2)));
            layoutParams.leftMargin = (int) getResources().getDimension(R.dimen.dp1);
            layoutParams.rightMargin = (int) getResources().getDimension(R.dimen.dp1);
            binding.recDotMenuLayout.addView(imageView, layoutParams);
        }
    }

    /**
     * 选中当前不的布局标识
     *
     * @param selectItems
     */
    private void setRecImageBackgroundMenu(int selectItems) {
        for (int i = 0; i < recMenuTip.length; i++) {
            if (i == selectItems) {
                recMenuTip[i].setSelected(true);
            } else {
                recMenuTip[i].setSelected(false);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding.recConsViewPage.setAdapter(null);
    }
}
